package io.kiki.sba.registry.server.data_store.remoting.dataserver.handler;

import io.kiki.sba.registry.common.model.slot.DataSlotDiffDigestRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;


public class SlotFollowerDiffDigestRequestHandler extends BaseSlotDiffDigestRequestHandler {
    private static final Logger logger = LoggerFactory.getLogger(SlotFollowerDiffDigestRequestHandler.class);

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    public SlotFollowerDiffDigestRequestHandler() {
        super();
    }

    @Override
    public Executor getExecutor() {
        return threadPoolExecutor;
    }

    @Override
    protected boolean preCheck(DataSlotDiffDigestRequest request) {
        if (!slotManager.isLeader(serverConfig.getDataCenter(), request.getSlotId())) {
            logger.warn("sync slot request from {}, not leader of {}", request.getLocalDataCenter(), request.getSlotId());
            return false;
        }
        return true;
    }

    @Override
    protected boolean postCheck(DataSlotDiffDigestRequest request) {
        return true;
    }
}
